package com.guragura.screens;

import org.lwjgl.Sys;

import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.guragura.Guragura;
import com.guragura.util.Constants;
import com.guragura.util.GuraFont;
import com.guragura.util.GuraMusic;
import com.guragura.util.GuraSound;
import com.guragura.widget.AnimatedSprite;
import com.guragura.widget.KabayanLoadingSprite;

/**
 * First screen that will be seen by player. In this screen, there are splash
 * screen and loading animation. Assets will be loaded asynchronously now.
 * 
 * @author hartono
 * 
 */
public class StartScreen extends GuraScreen {

	public TextureRegion[] splashImages;
	public TextureRegion background;

	/**
	 * jumlah splash screen
	 */
	private int page = 1;
	private double startTime;
	private double currTime;

	private KabayanLoadingSprite kabayanSprite;

	private AnimatedSprite loadingSprite;

	/**
	 * selang waktu antar splash screen
	 */
	private int deltaTime = 1000;

	public int i = 0;

	public StartScreen() {
		super();
	}

	public StartScreen(Guragura context) {
		super(context);
	}

	/* (non-Javadoc)
	 * @see com.guragura.screens.GuraScreen#initScreen()
	 * meload assets dulu disini
	 */
	@Override
	public void initScreen() {
		state = SCREEN_RUNNING;

		// load resource for this screen

		assetManager.load(Constants.PACK_START, TextureAtlas.class);
		assetManager.finishLoading();
		// init res for this screen
		guragura.atlasStart = assetManager.get(Constants.PACK_START,
				TextureAtlas.class);

		// init object for this screen
		loadingSprite = new AnimatedSprite(
				guragura.atlasStart.findRegions(Constants.START_LOADING_TEXT));

		kabayanSprite = new KabayanLoadingSprite(
				guragura.atlasStart.findRegion(Constants.START_KABAYAN_LOADING),
				3);

		kabayanSprite.setPosition(
				(320 - kabayanSprite.getWidth()) / 2, 240);

		loadingSprite.setPosition(
				(320- loadingSprite.getWidth()) / 2,
				kabayanSprite.getY() - loadingSprite.getHeight());

		background = guragura.atlasStart
				.findRegion(Constants.LOADING_AWAL_SCREEN);

		splashImages = new TextureRegion[1];
		splashImages[0] = guragura.atlasStart
				.findRegion(Constants.SPLASH_SCREEN);

		// load the other resources async
		loadRes();
	}

	@Override
	public void onScreenRunning() {
		// splash screen digambar disini
		currTime = System.currentTimeMillis();
		batch.begin();
		batch.draw(splashImages[page - 1], 0, 0);
		batch.end();
		if (currTime - startTime >= deltaTime) {
			nextSplash();
			startTime = currTime;
		}

	}

	@Override
	public void onScreenPause() {
	}

	@Override
	public void onScreenLoading() {
		if (assetManager.update()) {
			// finished loading
			// start initialize
			guragura.atlasInterface = assetManager.get(
					Constants.PACK_INTERFACE, TextureAtlas.class);
			
			guragura.sound = new GuraSound(assetManager);
			guragura.music = new GuraMusic(guragura);
			guragura.guraFont = new GuraFont(assetManager, batch);

			this.guragura.initGame();
			this.guragura.popScreen().dispose();
		} else {
			// still loading
			onLoadingAnimation();
		}
	}

	@Override
	public void show() {
		super.show();
		guragura.gameState = Guragura.START_SCREEN;
		startTime = System.currentTimeMillis();
	}

	/**
	 * mengganti ke page berikutnya / splash screen berikutnya
	 */
	public void nextSplash() {
		if (page < splashImages.length) {
			page++;
		} else {
			state = SCREEN_LOADING;
		}
	}

	/**
	 * animasi ketika loading 
	 */
	public void onLoadingAnimation() {

		batch.begin();
		batch.draw(background, 0, 0);
		loadingSprite.draw(batch);
		kabayanSprite.draw(batch);
		batch.end();

	}

	/**
	 * menentukan asset mana aja yang akan diload untuk game ini
	 */
	public void loadRes() {		
		assetManager.load(Constants.PACK_INTERFACE, TextureAtlas.class);
		assetManager.load("sfx/akifail.mp3", Sound.class);
		assetManager.load("bgm/mainmenu.mp3", Music.class);
		assetManager.load("bgm/play1.mp3", Music.class);
		assetManager.load("bgm/play2.mp3", Music.class);
		assetManager.load("bgm/gempa1.mp3", Music.class);
		assetManager.load("bgm/gempa2.mp3", Music.class);
		assetManager.load("bgm/napas.mp3", Music.class);
		
		assetManager.load("sfx/akifail.mp3",Sound.class);
		assetManager.load("sfx/akikena.mp3", Sound.class);
		assetManager.load("sfx/akitolong.mp3", Sound.class);
		assetManager.load("sfx/cling.mp3", Sound.class);
		assetManager.load("sfx/hitspecial.mp3", Sound.class);
		assetManager.load("sfx/ibufail.mp3", Sound.class);
		assetManager.load("sfx/ibukena.mp3", Sound.class);
		assetManager.load("sfx/ibutolong.mp3", Sound.class);
		assetManager.load("sfx/ototfail.mp3", Sound.class);
		assetManager.load("sfx/ototkena.mp3", Sound.class);
		assetManager.load("sfx/otottolong.mp3", Sound.class);
		assetManager.load("sfx/pencurifail.mp3", Sound.class);
		assetManager.load("sfx/pencurikena.mp3", Sound.class);
		assetManager.load("sfx/pencuritolong.mp3", Sound.class);
		assetManager.load("sfx/tereakjatoh.mp3", Sound.class);	
	}

	@Override
	public void initFromRes() {
		// TODO Auto-generated method stub

	}

}
